package org.csu.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.demo.common.CommonResponse;
import org.csu.demo.entity.Account;
import org.csu.demo.entity.BannerData;
import org.csu.demo.entity.Profile;
import org.csu.demo.entity.SignOn;
import org.csu.demo.persistence.AccountMapper;
import org.csu.demo.persistence.BannerDataMapper;
import org.csu.demo.persistence.ProfileMapper;
import org.csu.demo.persistence.SignOnMapper;
import org.csu.demo.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private BannerDataMapper bannerDataMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private SignOnMapper signOnMapper;

    public CommonResponse<AccountVO> login(String username, String password){
        QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        SignOn signOn = signOnMapper.selectOne(queryWrapper);
        if (signOn == null){
            return CommonResponse.creatForError("用户名或密码不正确");
        }
        return getAccount(username);
    }

    public CommonResponse<AccountVO> getAccount(String username){
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        String favouriteCategoryId = profile.getFavouriteCategoryId();
        BannerData bannerData = bannerDataMapper.selectById(favouriteCategoryId);
        SignOn signOn = signOnMapper.selectById(username);
        if (account == null){
            return CommonResponse.creatForError("获取用户信息失败");
        }
        AccountVO accountVO = entityToAccountVO( account, bannerData, profile, signOn);
        return CommonResponse.creatForSuccess(accountVO);
    }

    private AccountVO entityToAccountVO(Account account,BannerData bannerData,Profile profile,SignOn signOn){
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(account.getUsername());
        accountVO.setPassword(signOn.getPassword());
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setCountry(account.getCountry());
        accountVO.setZip(account.getZip());
        accountVO.setPhone(account.getPhone());

        accountVO.setLanguagePreference(profile.getLanguagePreference());
        accountVO.setBannerOption(profile.getBannerOption() == 1);
        accountVO.setListOption(profile.getListOption() == 1);
        if (profile.getBannerOption() == 1){
            accountVO.setFavouriteCategoryId(profile.getFavouriteCategoryId());
            accountVO.setBannerName(bannerData.getBannerName());
        }else{
            accountVO.setFavouriteCategoryId("");
            accountVO.setBannerName("");
        }
        return accountVO;
    }
}
